package com.awcall.iot.controller;


import com.awcall.iot.common.ResponseResult;
import com.awcall.iot.model.Role;
import com.awcall.iot.model.RoleAuthority;
import com.awcall.iot.service.IRoleAuthorityService;
import com.awcall.iot.service.IRoleService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.awcall.iot.common.BaseController;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author wuhui
 * @since 2018-03-05
 */
@RestController
@RequestMapping("/admin/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IRoleAuthorityService iRoleAuthorityService;

    @GetMapping("/index")
    public ResponseResult index(Page<Role> page) {
        Page<Role> rolePage = roleService.selectPage(page);
        return new ResponseResult<>(rolePage);
    }

    @GetMapping("/indexNoPage")
    public ResponseResult indexNoPage() {
        List<Role> list = roleService.selectList(null);
        return new ResponseResult<>(list);
    }

    @GetMapping("/detail")
    public ResponseResult detail(Role role) {
        Role role1 = roleService.selectById(role.getId());
        return new ResponseResult<>(role1);
    }

    @PostMapping("/create")
    public ResponseResult create(@RequestBody Role role) {
        return roleService.insertRole(role);
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody Role role) {
        return roleService.updateRole(role);
    }

    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody Role role) {
        boolean b = roleService.deleteById(role);
        if (b) {
            return new ResponseResult();
        } else {
            return new ResponseResult<>(1, "删除失败");
        }
    }

    @GetMapping("/show")
    public ResponseResult show(Long id) {
        List<RoleAuthority> authorityIdList = iRoleAuthorityService.findByRoleId(id);
        return new ResponseResult<>(authorityIdList);
    }

    @PostMapping("/updateAuthority")
    public ResponseResult updateAuthority(@RequestBody Role role) {
        return iRoleAuthorityService.updateAuthority(role);
    }

}
